/*    */ package client.inventory;
/*    */ 
/*    */ import com.alibaba.druid.pool.DruidPooledConnection;
/*    */ import database.DatabaseConnection;
/*    */ import java.io.Serializable;
/*    */ import java.sql.PreparedStatement;
/*    */ import java.sql.ResultSet;
/*    */ import java.sql.SQLException;
/*    */ import java.util.concurrent.atomic.AtomicInteger;
/*    */ 
/*    */ public class MapleInventoryIdentifier
/*    */   implements Serializable
/*    */ {
/*    */   private static final long serialVersionUID = 21830921831301L;
/* 15 */   private static final MapleInventoryIdentifier instance = new MapleInventoryIdentifier();
/* 16 */   private final AtomicInteger runningUID = new AtomicInteger(0);
/*    */   
/*    */   public static int getInstance() {
/* 19 */     return instance.getNextUniqueId();
/*    */   }
/*    */   
/*    */   public int getNextUniqueId() {
/* 23 */     if (this.runningUID.get() <= 0) {
/* 24 */       this.runningUID.set(initUID());
/*    */     } else {
/* 26 */       this.runningUID.set(this.runningUID.get() + 1);
/*    */     } 
/* 28 */     return this.runningUID.get();
/*    */   }
/*    */   
/*    */   public int initUID() {
/* 32 */     int ret = 0;
/* 33 */     try (DruidPooledConnection con = DatabaseConnection.getInstance().getConnection()) {
/* 34 */       int[] ids = new int[5];
/* 35 */       PreparedStatement ps = con.prepareStatement("SELECT MAX(uniqueid) FROM inventoryitems WHERE uniqueid > 0");
/* 36 */       ResultSet rs = ps.executeQuery();
/* 37 */       if (rs.next()) {
/* 38 */         ids[0] = rs.getInt(1) + 1;
/*    */       }
/* 40 */       rs.close();
/* 41 */       ps.close();
/*    */       
/* 43 */       ps = con.prepareStatement("SELECT MAX(petid) FROM pets");
/* 44 */       rs = ps.executeQuery();
/* 45 */       if (rs.next()) {
/* 46 */         ids[1] = rs.getInt(1) + 1;
/*    */       }
/* 48 */       rs.close();
/* 49 */       ps.close();
/*    */       
/* 51 */       ps = con.prepareStatement("SELECT MAX(ringid) FROM rings");
/* 52 */       rs = ps.executeQuery();
/* 53 */       if (rs.next()) {
/* 54 */         ids[2] = rs.getInt(1) + 1;
/*    */       }
/* 56 */       rs.close();
/* 57 */       ps.close();
/*    */       
/* 59 */       ps = con.prepareStatement("SELECT MAX(partnerringid) FROM rings");
/* 60 */       rs = ps.executeQuery();
/* 61 */       if (rs.next()) {
/* 62 */         ids[3] = rs.getInt(1) + 1;
/*    */       }
/* 64 */       rs.close();
/* 65 */       ps.close();
/*    */       
/* 67 */       ps = con.prepareStatement("SELECT MAX(uniqueid) FROM androids");
/* 68 */       rs = ps.executeQuery();
/* 69 */       if (rs.next()) {
/* 70 */         ids[4] = rs.getInt(1) + 1;
/*    */       }
/* 72 */       rs.close();
/* 73 */       ps.close();
/*    */       
/* 75 */       for (int id : ids) {
/* 76 */         if (id > ret) {
/* 77 */           ret = id;
/*    */         }
/*    */       } 
/* 80 */     } catch (SQLException e) {
/* 81 */       e.printStackTrace();
/*    */     } 
/* 83 */     return ret;
/*    */   }
/*    */ }


/* Location:              C:\PGX\Server.jar!\client\inventory\MapleInventoryIdentifier.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */